# -*- coding: utf-8 -*-
# @time: 2024/6/5 14:09
# @file: price_median
# @author: tyshixi08

from get_data.origin_data import *

# 获取转债价格
def get_bond_price(code=code_ls(), start_date = month_ls()[0].replace('-','') + '01', end_date = month_ls()[-1].replace('-','') + '01'):
    df = get_price(code, start_date, end_date, fields='close').reset_index()
    df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
    return df

# 获取转债价格中位数时间序列
def get_price_median(code=code_ls(), start_date = month_ls()[0].replace('-','') + '01', end_date = month_ls()[-1].replace('-','') + '01'):
    df = get_bond_price(code, start_date, end_date)
    df_price_median = df.groupby('date')['close'].median().reset_index().rename(columns={'close':'covprice_mid'})
    df_price_median['date'] = pd.to_datetime(df_price_median['date']).dt.strftime('%Y-%m-%d')
    return df_price_median.sort_values('date')

# 数据存档
def covprice_save():
    excel_file_path = 'covprice_mid.csv'
    #if os.path.exists('原始数据/' + excel_file_path):
    #    df = pd.read_csv('原始数据/' + excel_file_path)
    #    return df
    #else:
    save_CSV(get_price_median(), 'get_data/原始数据/' + excel_file_path.split('.')[0])
    df = pd.read_csv('get_data/原始数据/' + excel_file_path)
    return df
